package com.ego.admin.controller;

import com.ego.admin.po.SysNotice;
import com.ego.admin.service.SysNoticeService;
import com.ego.admin.vo.SysNoticeSearchVo;
import com.ego.common.annotation.ControllerEndpoint;
import com.ego.common.util.Result;
import com.ego.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;

/**
 * @program: ego
 * @description: 系统公告控制器
 * @author: KamTang
 * @create: 2022-11-16 19:54
 **/
@RestController
@RequestMapping("/sys/notice")
public class SysNoticeController {

    @Autowired
    private SysNoticeService noticeService;

    /**
     * 分页展示公告
     */
    @PostMapping("/search")
    public Result search (@Valid @RequestBody SysNoticeSearchVo search) {
        PageVo<SysNotice> page =  noticeService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 添加公告
     */
    @ControllerEndpoint(exceptionMessage = "新增公告失败", operation = "新增公告")
    @PostMapping("/save")
    public Result save (@Valid @RequestBody SysNotice notice) {
        boolean flag = noticeService.save(notice);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除
     */
    @ControllerEndpoint(exceptionMessage = "删除公告失败", operation = "删除公告")
    @PostMapping("/delete")
    public Result deleteBatch(@Valid @RequestBody Long[] ids) {
        boolean flag = noticeService.removeByIds(Collections.singletonList(ids));
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更新
     */
    @ControllerEndpoint(exceptionMessage = "更新公告信息失败", operation = "更新公告信息")
    @PostMapping("/update")
    public Result update(@Valid @RequestBody SysNotice notice) {
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

